<?php

namespace backend\controllers;

use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\captcha\Captcha;
use common\models\Setting\Setting;
use common\models\adminlog\AdminLog;

class YanzhengController extends BackendController
{
	public function actions()
    {
		$setting = Setting::findBySql('SELECT value FROM setting where id in(52,53,54)')->asArray()->all();
		$captchaConfig = array(
        'class' => 'yii\captcha\CaptchaAction',
        'height' => $setting[2]['value'],
        'width' => $setting[1]['value'],
        'maxLength' => 4,
        'minLength' => 4,
		'flag' => $setting[0]['value'],
		); 
		
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
			'captcha' => $captchaConfig, 
        ];
    }

    public function actionIndex()
    {
		$setting = Setting::findBySql('SELECT value FROM setting where id in(51,52,53,54)')->asArray()->all();
        return $this->render('index', [
            'setting' => $setting,
        ]);
    }
	
	public function actionUpdate(){
		 if(Yii::$app->request->isPost){
			$data = Yii::$app->request->post();
			$captcha = implode(",", $data['captcha']);
			$captcha_type = $data['captcha_type'];
			$captcha_width = $data['captcha_width'];
			$captcha_height = $data['captcha_height'];
			Setting::updateAll(['value'=>$captcha],['id' =>51]);
			Setting::updateAll(['value'=>$captcha_type],['id' =>52]);
			Setting::updateAll(['value'=>$captcha_width],['id' =>53]);
			Setting::updateAll(['value'=>$captcha_height],['id' =>54]);
			Adminlog::addlog(yii::$app->user->id, "修改验证码操作");
			yii::$app->getSession()->setFlash('success','保存成功！');
		 }
		 
		 return $this->redirect(['yanzheng/index']);
	}
}
